package o2o.thridpost.dada.controller;

import com.jinyou.utils.common.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import o2o.config.PropConfigure;
import o2o.config.thirdpost.dada.DadaRequestClient;
import o2o.thridpost.dada.client.DadaApiResponse;
import o2o.thridpost.dada.client.DadaRequestParam;
import o2o.thridpost.dada.config.ErrorCode;
import o2o.thridpost.dada.config.UrlConstant;
import o2o.thridpost.dada.model.to.AddDadaMerchantTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 达达们门店控制
 * @author sunyufan deporation@qq.com
 * @date 2021/5/28 11:48
 * @description: DadaMerchantController
 */
@RequestMapping("/dada/merchant")
@RestController
@Slf4j
public class DadaMerchantController {

    @Autowired
    private DadaRequestClient requestClient ;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addMerchant(@RequestBody AddDadaMerchantTO addDadaMerchantTO ){

        DadaRequestParam<AddDadaMerchantTO> requestParam = new DadaRequestParam<>(addDadaMerchantTO, requestClient.getAppConfig());
        requestParam.setSourceId("");

        DadaApiResponse apiResponse = requestClient.callRpc(requestParam, UrlConstant.MERCHANT_ADD_URL);
        log.info(apiResponse.toString());

        if (apiResponse.getCode() ==  0){
            Integer merchantId =  Integer.valueOf(apiResponse.getResult().toString());
            System.out.println(merchantId);
            Map<String,Integer> res = new HashMap<>(1);
            res.put("merchantId",merchantId);
            return AjaxResult.successResult(res);
        }else{
            return AjaxResult.errorResult("msg",ErrorCode.getInstance().getErrorCode().get(apiResponse.getCode()));
        }
    }
}
